package com.media.dao.impl;

import com.media.bean.UserBean;
import com.media.dao.UserDao;
import com.media.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {

    @Override
    public UserBean getUserById(int id) {

        UserBean userBean = new UserBean();

        try{
            Connection connection = DBUtil.getConnection();

            String sql = "select * from user where id = ?";

            PreparedStatement ps = connection.prepareStatement(sql);

            ps.setInt(1,id);

            ResultSet rs = ps.executeQuery();

            if(rs.next()){
                userBean.setId(rs.getInt("id"));
                userBean.setUsername(rs.getString("username"));
                userBean.setPassword(rs.getString("password"));
                userBean.setGender(rs.getInt("gender"));
                userBean.setAge(rs.getInt("age"));
                userBean.setStatus(rs.getInt("status"));
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return userBean;
    }

    @Override
    public UserBean getUserByName(String username) {
        UserBean userBean = new UserBean();

        try{
            Connection connection = DBUtil.getConnection();

            String sql = "select * from user where username = ?";

            PreparedStatement ps = connection.prepareStatement(sql);

            ps.setString(1,username);

            ResultSet rs = ps.executeQuery();

            if(rs.next()){
                userBean.setUsername(rs.getString("username"));
                userBean.setPassword(rs.getString("password"));
                userBean.setGender(rs.getInt("gender"));
                userBean.setAge(rs.getInt("age"));
                userBean.setStatus(rs.getInt("status"));
                userBean.setId(rs.getInt("id"));
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return userBean;
    }

    @Override
    public List<UserBean> getUserList(UserBean user) {

        List<UserBean> userList = new ArrayList<>();
        UserBean userBean = null;

        try{
            Connection connection = DBUtil.getConnection();

            String sql = "select * from user where username like ?";

            PreparedStatement ps = connection.prepareStatement(sql);

            ps.setString(1,user.getUsername());

            ResultSet rs = ps.executeQuery();

            while (rs.next()){
                userBean = new UserBean();
                userBean.setUsername(rs.getString("username"));
                userBean.setPassword(rs.getString("password"));
                userBean.setGender(rs.getInt("gender"));
                userBean.setAge(rs.getInt("age"));
                userBean.setId(rs.getInt("id"));
                userBean.setStatus(rs.getInt("status"));
                userList.add(userBean);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return userList;
    }

    @Override
    public int addUser(UserBean user) throws SQLException {
        Connection connection = DBUtil.getConnection();

        String sql = "inert into user (username,password,age,gender,status) values (?,?,?,?,?)";

        PreparedStatement ps = connection.prepareStatement(sql);

        ps.setString(1,user.getUsername());
        ps.setString(2,user.getPassword());
        ps.setInt(3,user.getAge());
        ps.setInt(4,user.getGender());
        ps.setInt(5,user.getStatus());

        int count = ps.executeUpdate();

        DBUtil.close(connection,ps,null);

        return count;
    }

    @Override
    public int editUser(UserBean user) {
        return 0;
    }

    @Override
    public int deleteUserById(int id) {
        return 0;
    }
}
